package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/Order")
public class OrderController {
    @Reference
    private OrderService orderService;
@Autowired
private JedisPool jedisPool;
    @RequestMapping("/saveOrder")
    public Result saveOrder(@RequestBody Map map){
        try {
            //1、校验 验证码
            String validateCode = (String) map.get("validateCode");
            String telephone = (String) map.get("telephone");
            //从redis里面取出验证码
            String redisValidateCode=jedisPool.getResource().get(telephone+ RedisMessageConstant.SENDTYPE_ORDER);
            if(redisValidateCode==null){//过期或者没有发送校验码
                return new Result(false, MessageConstant.VALIDATECODE_NOTNULL_ERROR);
            }else{
                if(!redisValidateCode.equalsIgnoreCase(validateCode)){//校验码不等 有问题
                    return new Result(false,MessageConstant.VALIDATECODE_ERROR);
                }else{//验证码没问题
                    Result result=orderService.saveOrder(map);
                   return result;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map=orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
